package com.campus.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.activity.entity.Organizer;
import com.campus.activity.mapper.OrganizerMapper;
import com.campus.activity.service.OrganizerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 组织者服务实现类
 * 
 * @author System
 * @since 2024
 */
@Service
public class OrganizerServiceImpl extends ServiceImpl<OrganizerMapper, Organizer> implements OrganizerService {

    @Autowired
    private OrganizerMapper organizerMapper;

    @Override
    public boolean addOrganizer(Organizer organizer) {
        // 检查联系方式是否已存在
        if (isContactExists(organizer.getContactInfo())) {
            return false;
        }
        
        organizer.setCreateTime(LocalDateTime.now());
        organizer.setUpdateTime(LocalDateTime.now());
        organizer.setDeleted(0);
        return save(organizer);
    }

    @Override
    public boolean updateOrganizer(Integer organizerId, Organizer organizer) {
        organizer.setOrganizerId(organizerId);
        organizer.setUpdateTime(LocalDateTime.now());
        return updateById(organizer);
    }

    @Override
    public boolean deleteOrganizer(Integer organizerId) {
        Organizer organizer = new Organizer();
        organizer.setOrganizerId(organizerId);
        organizer.setDeleted(1);
        organizer.setUpdateTime(LocalDateTime.now());
        return updateById(organizer);
    }

    @Override
    public List<Organizer> getOrganizersByName(String organizerName) {
        return organizerMapper.selectByOrganizerName(organizerName);
    }

    @Override
    public Organizer getOrganizerByContact(String contactInfo) {
        return organizerMapper.selectByContactInfo(contactInfo);
    }

    @Override
    public boolean isContactExists(String contactInfo) {
        return getOrganizerByContact(contactInfo) != null;
    }

    @Override
    public List<Organizer> getAllOrganizers() {
        QueryWrapper<Organizer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0)
                   .orderByAsc("organizer_name");
        return list(queryWrapper);
    }
}